<?php
namespace app\dsadmin\controller;
use app\dsadmin\model\MemberModel;
use app\common\model\EnameModel;
use think\Db;
use think\Cache;

class Member extends Base
{
    public function index()
    {
        $key = input('key');
         
        // $lists = Db::name('member')->where(['status' => 1,'ename_first' => "www.jiujiucar.com.cn"])->select();
        
        // foreach ($lists as $v){
            
        //     // if(!empty($v['password'])){
        //     //      $members = Db::name('member')->where(['id' => $v['id']])->update(['password' => md5($v['password'])]);
        //     // }
            
        //     $day   = date("Y-m-d");
        //     $zuori = date("Y-m-d", strtotime("$day-1 day"));
        //     $mapzf['shijian'] = ['like', "%" . $zuori . "%"];
        //     $mapzf['userid']  = $v['id'];
        //     $money2 = Db::name("dingdan")->where($mapzf)->sum('money');
            
        //     if($money2 <= 500)
        //     {
        //         if(1 == rand(1,5)){
        //              $ename = 'www.sccdsdz008.cn';
        //              Db::name('member')->where(['id' => $v['id']])->update(['ename_first' => $ename]);
        //         }
                
        //         // if(1 == rand(1,5)){
        //         //      $ename = 'www.sunjianming.cn';
        //         //      Db::name('member')->where(['id' => $v['id']])->update(['ename_first' => $ename]);
        //         // }
                
               
        //     } 
           
         
           
        // }
        // var_dump(111);
        // die;

        $map['closed'] = 0; //0δɾ����1��ɾ��
        if ($key && $key !== "")
        {
            $map['id|account|nickname|qq|weixin'] = ['=', $key ];
        }
        
        $money = Db::name('member')->where(['status' => 1])->sum('money');
       

        $member = new MemberModel();

        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 10; // ��ȡ������
        $count = $member->getAllCount($map); //������ҳ��
        
        $allpage = intval(ceil($count / $limits));

        $lists = $member->getMemberByWhere($map, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); //��ǰҳ
        $this->assign('allpage', $allpage); //��ҳ�� 
        $this->assign('val', $key);
        $this->assign('money', $money);
         $zuo      = strtotime(date("Y-m-d",strtotime("-1 day")));
        $zuoe     = strtotime(date("Y-m-d"));
        $wherez['shijian'] = ['between',date('Y-m-d H:i:s',$zuo).','.date('Y-m-d H:i:s',$zuoe)];
       
        if (input('get.page'))
        {
             foreach ($lists as &$v){
                $wherez['userid'] =  $v['id'];
                $money = Db::name('dingdan')->where($wherez)->sum('money');
                $count = Db::name('dingdan')->where($wherez)->count('id');
                $zong_name = Db::name('member')->where(['id'=>$v['zong_id']])->value('account');
                $v['zong_id'] = $zong_name;
                $v['count']  = $count;
                $v['zmoney'] = $money;
                $v['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
                unset($v['password']);
                unset($v['txpassword']);
            }
            unset($v);
            return json($lists);
        }

        return $this->fetch();
    }
    
    
    public function zongdai()
    {
        $key = input('key');
        
        $map['closed'] = 0; //0δɾ����1��ɾ��
        if ($key && $key !== "")
        {
            $map['nickname|weixin'] = ['=', $key ];
        }
        
        $money = Db::name('member')->where(['status' => 1])->sum('money');
       

        $member = new MemberModel();

        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits = 1000; // ��ȡ������
        $count = $member->getAllCount($map); //������ҳ��
        
        $allpage = intval(ceil($count / $limits));

        $lists = $member->getMemberByWhere($map, $Nowpage, $limits);

        $this->assign('Nowpage', $Nowpage); //��ǰҳ
        $this->assign('allpage', $allpage); //��ҳ�� 
        $this->assign('val', $key);
        $this->assign('money', $money);
        $zuo      = strtotime(date("Y-m-d",strtotime("-1 day")));
        $zuoe     = strtotime(date("Y-m-d"));
        $wherez['shijian'] = ['between',date('Y-m-d H:i:s',$zuo).','.date('Y-m-d H:i:s',$zuoe)];
        $zonge = 0;
         foreach ($lists as &$v){
            $wherez['userid'] =  $v['id'];
            $money = Db::name('dingdan')->where($wherez)->sum('money');
            $count = Db::name('dingdan')->where($wherez)->count('id');
            // $status_zong = Db::name('member')->where(['id'=>$v['id']])->value('status_zong');
            // $v['status_zong']  = $status_zong;
            $v['count']  = $count;
            $v['zmoney'] = round($money,1);
            $v['last_login_time'] = date('Y-m-d H:i:s',$v['last_login_time']);
            $zonge += $money;
            unset($v['password']);
            unset($v['txpassword']);
        }
          unset($v);
         $this->assign('lists', $lists);
         $this->assign('zonge', $zonge);
        

        return $this->fetch();
    }
    
    
    
    public function dingorder_bi(){
        $lists = Db::name('member')->where(['id' => ['neq',10027],'status' => 1])->select();
        $todaytime = date('Y-m-d H:i:s',strtotime(date("Y-m-d"),time()));
        $day_time["1"] = date('Y-m-d H:i:s',strtotime($todaytime)-86400);
        $day_time["2"] = date('Y-m-d H:i:s',strtotime($todaytime)-172800);
        $day_time["3"] = date('Y-m-d H:i:s',strtotime($todaytime)-259200);
        $where1['shijian'] = ['between',$day_time['1'] .','.$todaytime]; //昨天
        $where2['shijian'] = ['between',$day_time['2'].','.$day_time['1']];//前天
        $where3['shijian'] = ['between',$day_time['3'].','.$day_time['2']];//大前天
        foreach ($lists as &$v){
            $where1['userid']  =  $v['id'];
            $where2['userid']  =  $v['id'];
            $where3['userid']  =  $v['id'];
            $money1 = Db::name('dingdan')->where($where1)->sum('money');
            $money2 = Db::name('dingdan')->where($where2)->sum('money');
            $money3 = Db::name('dingdan')->where($where3)->sum('money');
            $v['money1'] = round($money1);
            $v['money2'] = round($money2);  
            $v['money3'] = round($money3);  
           
            $data['userid'] = $v['id'];
            $data['money']  = $v['money'];
            $data['money1'] = $v['money1'];
            $data['money2'] = $v['money2'];
            $data['money3'] = $v['money3'];
            $data['zfcha']  = $v['money1']-$v['money2'];
            $data['account']   = $v['account'];
            $data['nickname']   = $v['nickname'];
            $fla = Db::name('order_bi')->where(['userid' => $v['id']])->find();
            if(!empty($fla)){
                $datas['money']  = $v['money'];
                $datas['money1'] = $v['money1'];
                $datas['money2'] = $v['money2'];
                $datas['money3'] = $v['money3'];
                Db::name('order_bi')->where(['userid' =>$v['id']])->update($datas);
            }else{
                Db::name('order_bi')->insert($data); 
            }
       }
    }
     public function shengorder(){
        $key  = input('key');
        $map = [];
        if ($key && $key !== "")
        {
            $map['e.ename'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 100; // 获取总条数
        $count = Db::name('order_bi')->where($map)->count(); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $ename = new MemberModel();
        $lists = $ename->shengorderwhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        if (input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
     }
     
     public function jiangorder(){
        $key  = input('key');
        $map = [];
        if ($key && $key !== "")
        {
            $map['e.ename'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = 100; // 获取总条数
        $count = Db::name('order_bi')->where($map)->count(); //计算总页面
        $allpage = intval(ceil($count / $limits));
        $ename = new MemberModel();
        $lists = $ename->jiangorderwhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数 
        $this->assign('val', $key);
        if (input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
     }
     
     
     
  //代理余额比
    public function order(){
       $time = time() - 182800;
      
       $lists = Db::name('member')->where(['id' => ['neq',10027],'status' => 1,'last_login_time' => ['gt',$time]])->select();
      
       $jin     = strtotime(date("Y-m-d"));
       $jintime = time();
       $zuo     = strtotime(date("Y-m-d",strtotime("-1 day")));
       $zuoe    = $jintime - 86400;
       $arr = '';
       foreach ($lists as &$v){
           $wherej['shijian'] = ['between',date('Y-m-d H:i:s',$jin) .','.date('Y-m-d H:i:s',$jintime)];
           $wherez['shijian'] = ['between',date('Y-m-d H:i:s',$zuo).','.date('Y-m-d H:i:s',$zuoe)];
           $wherez['userid']  =  $v['id'];
           $wherej['userid']  =  $v['id'];
           $jmoney = Db::name('dingdan')->where($wherej)->sum('money');
           $zmoney = Db::name('dingdan')->where($wherez)->sum('money');
          
           $v['zmoney'] = round($zmoney);
           $v['jmoney'] = round($jmoney);  
          
          
            // $arr[]['zmoney'] = $zmoney;
            // $arr[]['jmoney'] = $jmoney;
            // $arr[]['userid'] = $wherez['userid'];
        
       }
       unset($v);
       $ages = array();
        foreach ($lists as $user) {
           
          
            $ages[] = $user['jmoney'];
          
             
           
           
        }
        array_multisort($ages, SORT_DESC, $lists);
        
        $Nowpage = input('get.page') ? input('get.page'):1;

        $limits  = 300; // ��ȡ������
        $count  =  10000000; //������ҳ��
        
        $allpage = intval(ceil($count / $limits));


        $this->assign('Nowpage', $Nowpage); //��ǰҳ
        $this->assign('allpage', $allpage); //��ҳ�� 
        
         if (input('get.page'))
        {
            return json($lists);
           
        }
        
         return $this->fetch();
        
    }
 
    


    public function add_member()
    {
        if (request()->isAjax())
        {
            $param = input('post.');
            unset($param['file']);
            $member = new MemberModel();
            // $param['txfeilv'] = 30;
            $flag = $member->insertMember($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $ename = new EnameModel();
        $zong_nname = Db::name('member')->where(['status'=>1,'status_zong'=>1])->select();
        $level1 = $ename->getEnamesByType(['type' => 1]);
        $level2 = $ename->getEnamesByType(['type' => 2]);
        $this->assign([
            'level1' => $level1,
            'level2' => $level2,
            'zong_nname' => $zong_nname
        ]);

        return $this->fetch();
    }

    public function edit_member()
    {
        $member = new MemberModel();
        if (request()->isAjax())
        {
            $param = input('post.');
            // $param['txfeilv'] = 30;
            $members = Db::name("member")->where(['id' => $param['id']])->find();
        
            $flag = $member->editMember($param);
            
            Cache::store('redis')->set("ylmember".$members['id'],json_encode($members));
            
            
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $ename = new EnameModel();
        $level1 = $ename->getEnamesByType(['type' => 1,'status' => 1]);
        
        $zong_nname = Db::name('member')->where(['status'=>1,'status_zong'=>1])->select();
        $level2 = $ename->getEnamesByType(['type' => 2]);

        $id = input('param.id');

        $this->assign([
            'member' => $member->getOneMember($id),
            'level1' => $level1,
            'level2' => $level2,
            'zong_nname' => $zong_nname
            ]);
        return $this->fetch();
    }

    public function del_member()
    {
        $id = input('param.id');
        $member = new MemberModel();
        $flag = $member->delMember($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
    
    
    public function edit_txpassword()
    {   
        $param = input('post.');
        $twopassword = md5(md5($param['twopassword']) . config('auth_key'));
        $txpassword = md5($param['txpassword']);
        $uid = session('uid');
        $hasUser = Db::name('admin')->where('id', $uid)->find();
        if($hasUser['twopassword'] != $twopassword)
        {
            return json(['code' => 0, 'data' => '', 'msg' => "安全密码不正确!!!"]);
        }
        $member = new MemberModel();
        $flag = $member->edittxpassword($param['id'],$txpassword);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
    
    public function edit_password()
    {   
        $param = input('post.');
        $twopassword = md5(md5($param['twopassword']) . config('auth_key'));
        $password    = md5($param['password']);
        $uid = session('uid');
        $hasUser = Db::name('admin')->where('id', $uid)->find();
        if($hasUser['twopassword'] != $twopassword)
        {
            return json(['code' => 0, 'data' => '', 'msg' => "安全密码不正确!!!"]);
        }
        $member = new MemberModel();
        $flag = $member->editpassword($param['id'],$password);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }
    
    public function edit_jiefeng()
    {   
        $param = input('post.');
        $twopassword = md5(md5($param['twopassword']) . config('auth_key'));
        $uid = session('uid');
        $hasUser = Db::name('admin')->where('id', $uid)->find();
        if($hasUser['twopassword'] != $twopassword)
        {
            return json(['code' => 0, 'data' => '', 'msg' => "安全密码不正确!!!"]);
        }
        try
        {
            $member = Db::name('member')->where(['id'=>$param['id']])->find();
            Db::name('dlfj')->where(['name' => $member['account']])->delete();
            return ['code' => 1, 'data' => '', 'msg' => '解封成功'];
        }
        catch(PDOException $e)
        {
            return ['code' => 0, 'data' => '', 'msg' => '解封失败'];
        }
    }

    public function del_member_txmoney()
    {
        $member = new MemberModel();
        $flag = $member->delMembertxmoney();
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    public function member_status()
    {
        $id = input('param.id');
        $status = Db::name('member')->where('id', $id)->value('status'); //�жϵ�ǰ״̬��� 
        if ($status == 1)
        {
            $flag = Db::name('member')->where('id', $id)->setField(['status' => 0]);
            
             $members = Db::name("member")->where(['id' => $id])->find();
            
            Cache::store('redis')->set("ylmember".$id,json_encode($members));
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '禁用成功']);
        }
        else
        {
            $flag = Db::name('member')->where('id', $id)->setField(['status' => 1]);
            $members = Db::name("member")->where(['id' => $id])->find();
            
            Cache::store('redis')->set("ylmember".$id,json_encode($members));
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '�ѿ���']);
        }
    }
    
      public function status_zong()
    {
        $id = input('param.id');
        $status = Db::name('member')->where('id', $id)->value('status_zong'); //�жϵ�ǰ״̬��� 
        if ($status == 1)
        {
            $flag = Db::name('member')->where('id', $id)->setField(['status_zong' => 0]);
            
          
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '禁用成功']);
        }
        else
        {
            $flag = Db::name('member')->where('id', $id)->setField(['status_zong' => 1]);
        
           
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '开启成功']);
        }
    }
}
